package br.unesp.demac.ccn07.clientes.apresentacao;
import br.unesp.demac.ccn07.clientes.dao.ClientDAO;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class ClienteTable extends JFrame implements ActionListener, TableModelListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JButton btAdicionar;
	private JTable table;
	private ResultSetTableModel model;

	public ClienteTable() throws ClassNotFoundException, SQLException {
		
		JPanel menubar = new JPanel(new FlowLayout(FlowLayout.LEFT));
		menubar.add(btAdicionar = new JButton("+"));
		btAdicionar.addActionListener(this);
		
		ClientDAO dao = new ClientDAO();
		ResultSet rs = dao.clientsToTable();
		model = new ResultSetTableModel(rs);

		table = new JTable(model);
		table.setGridColor(Color.gray);
		JScrollPane scrollPane = new JScrollPane(table);
		
		setTitle("Cadastro de Clientes");
		Container cp = getContentPane();
		cp.add(menubar, "North");
		cp.add(scrollPane);
		setSize(800, 400);
	}
	
	public void refreshTable() throws ClassNotFoundException, SQLException {
		ClientDAO dao = new ClientDAO();
		ResultSet rs = dao.clientsToTable();
		model = new ResultSetTableModel(rs);
		table.setModel(model);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btAdicionar) {
			new ClienteForm(this).setVisible(true);
		}
		
	}
	
	@Override
	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
		
	}
	

}
